﻿using System;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using QuickTest.Core.Compilation;
using QuickTest.Core.Events;
using QuickTest.Core.TestRunner;

namespace QuickTest.Tests.Core.TestRunner
{
    [TestFixture]
    public class EventHubTests
    {
        private EventHub eventHub;

        [SetUp]
        public void SetUp()
        {
            eventHub = new EventHub();
        }

        [Test]
        public void Can_Create_EventHub()
        {
            Assert.That(eventHub, Is.InstanceOfType(typeof(EventHub)), "Should create EventHub object");
        }

        [Test]
        public void can_raise_test_run_executed_event()
        {
            EventArgs actualArgs = null;
            eventHub.TestRunExecuted += (sender, e) => actualArgs = e;

            var expectedArgs = new EventArgs();
            eventHub.RaiseTestRunExecuted(expectedArgs);

            Assert.That(actualArgs, Is.EqualTo(expectedArgs));
        }

        [Test]
        public void can_raise_compile_started_event()
        {
            CompilerEventArgs actualArgs = null;
            eventHub.CompileStarted += (sender, e) => actualArgs = e;

            var expectedArgs = new CompileStartedEventArgs(string.Empty);
            eventHub.RaiseCompileStarted(expectedArgs);

            Assert.That(actualArgs, Is.EqualTo(expectedArgs));
        }

        [Test]
        public void can_raise_compile_finished_event()
        {
            CompilerEventArgs actualArgs = null;
            eventHub.CompileFinished += (sender, e) => actualArgs = e;

            var expectedArgs = new CompileFinishedEventArgs(string.Empty, true);
            eventHub.RaiseCompileFinished(expectedArgs);

            Assert.That(actualArgs, Is.EqualTo(expectedArgs));
        }

        [Test]
        public void can_raise_compile_error_event()
        {
            CompilerEventArgs actualArgs = null;
            eventHub.CompileError += (sender, e) => actualArgs = e;

            var expectedArgs = new CompileErrorEventArgs(string.Empty);
            eventHub.RaiseCompileError(expectedArgs);

            Assert.That(actualArgs, Is.EqualTo(expectedArgs));
        }

        [Test]
        public void Can_Raise_SuiteStarted_Event()
        {
            TestRunStartedEventArgs verifyArgs = null;
            eventHub.RunStarted += (sender, e) => verifyArgs = e;

            var testArgs = new TestRunStartedEventArgs(10);
            eventHub.RaiseRunStarted(testArgs);

            Assert.That(verifyArgs, Is.EqualTo(testArgs), "Should raise RunStarted event");
        }

        [Test]
        public void Can_Raise_TestFinished_Event()
        {
            TestEventArgs verifyArgs = null;
            eventHub.TestFinished += (sender, e) => verifyArgs = e;

            var testArgs = new TestEventArgs(new TestResult(String.Empty, ResultState.Success));
            eventHub.RaiseTestFinished(testArgs);

            Assert.That(verifyArgs, Is.EqualTo(testArgs), "Should raise TestFinished event");
        }

        [Test]
        public void Can_Raise_SuiteFinished_Event()
        {
            TestRunFinishedEventArgs verifyArgs = null;
            eventHub.RunFinished += (sender, e) => verifyArgs = e;

            var testArgs = new TestRunFinishedEventArgs(10, 5, 5, 0);
            eventHub.RaiseRunFinished(testArgs);

            Assert.That(verifyArgs, Is.EqualTo(testArgs), "Should raise RunFinished event");
        }
    }
}